/**
 * 询价.COM v1.0.0
 * http://xun-jia.com/
 *
 * Copyright (c) 2009 
 * 四川漾天空网络科技有限公司
 * 成都市天府软件园A3栋2楼
 *
 */
package com.xunjia.manage.action;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.tree.FlyweightCDATA;

import com.opensymphony.xwork2.ActionSupport;
import com.xunjia.util.Constants;
import com.xunjia.util.XMLNodeConstants;

/**
 * 询价网请求广告API
 * 
 * @author Mark
 * 
 * @Email Mark_yang@xun-jia.com
 * 
 * @Date 2009-4-17 下午01:29:22
 * 
 */
public class RquestAdvertisementDataAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	// 序列码
	private static final long serialVersionUID = 6735865166305509413L;
	// HTTP响应
	private HttpServletResponse response;
	// HTTP请求
	private HttpServletRequest request;

	/**
	 * 获取首页广告
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getIndexAdvertisement() throws Exception {
		response.setContentType(Constants.HTTP_HEAD_CONTENT_TYPE);
		request.setCharacterEncoding(Constants.CHARACTER_UTF8);
		response.setHeader(Constants.HTTP_HEAD_PRAGMA, Constants.HTTP_HEAD_NO_CACHE);
		response.setHeader(Constants.HTTP_HEAD_CACHE_CONTROL, Constants.HTTP_HEAD_NO_CACHE);
		response.setHeader(Constants.HTTP_HEAD_EXPIRES, "0");
		Element root = DocumentHelper.createElement(XMLNodeConstants.ROOT_ELEMENT_ADVERTISEMENT);
		Document doc = DocumentHelper.createDocument(root);

		Element imgs = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_TYPE_IMAGES);
		Element words = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_TYPE_WORDS);

		Element img = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_IMAGE);
		img.addAttribute(XMLNodeConstants.ATTRIBUTE_HREF, "http://www.sina.com.cn");
		img.addAttribute(XMLNodeConstants.ATTRIBUTE_SRC, "localhost/img/abc.jpg");
		Element text = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_TEXT);
		text.add(new FlyweightCDATA("中华人民共和国"));
		img.add(text);
		imgs.add(img);

		Element word = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_WORD);
		word.addAttribute(XMLNodeConstants.ATTRIBUTE_HREF, "http://cn.mail.yahoo.com");
		Element text1 = DocumentHelper.createElement(XMLNodeConstants.ELEMENT_ADVERTISEMENT_TEXT);
		text1.add(new FlyweightCDATA("美利坚合众国"));
		word.add(text1);
		words.add(word);

		root.add(imgs);
		root.add(words);

		OutputStream out = response.getOutputStream();
		// 写出XML文件
		out.write(doc.asXML().getBytes("UTF-8"));
		// 刷新二进制流缓存
		out.flush();
		// 关闭流
		out.close();
		return null;
	}

	/**
	 * 获取搜索结果面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getSearchAdvertisement() throws Exception {
		response.setContentType(Constants.HTTP_HEAD_CONTENT_TYPE);
		request.setCharacterEncoding(Constants.CHARACTER_UTF8);
		response.setHeader(Constants.HTTP_HEAD_PRAGMA, Constants.HTTP_HEAD_NO_CACHE);
		response.setHeader(Constants.HTTP_HEAD_CACHE_CONTROL, Constants.HTTP_HEAD_NO_CACHE);
		response.setHeader(Constants.HTTP_HEAD_EXPIRES, "0");

		OutputStream out = response.getOutputStream();
		// 写出XML文件
		// out.write(doc.asXML().getBytes("UTF-8"));
		// 刷新二进制流缓存
		out.flush();
		// 关闭流
		out.close();
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
